package com.eva.service.system.impl;

import com.eva.core.model.PageData;
import com.eva.core.model.PageWrap;
import com.eva.core.utils.Utils;
import com.eva.dao.system.SystemJobSnippetMapper;
import com.eva.dao.system.model.SystemJobSnippet;
import com.eva.service.system.SystemJobSnippetService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 定时任务片段Service实现
 * @author Eva.Caesar Liu
 * @date 2022/03/01 21:05
 */
@Service
public class SystemJobSnippetServiceImpl implements SystemJobSnippetService {

    @Autowired
    private SystemJobSnippetMapper systemJobSnippetMapper;

    @Override
    public Integer create(SystemJobSnippet systemJobSnippet) {
        systemJobSnippetMapper.insert(systemJobSnippet);
        return systemJobSnippet.getId();
    }

    @Override
    public void deleteById(Integer id) {
        systemJobSnippetMapper.deleteById(id);
    }

    @Override
    public void delete(SystemJobSnippet systemJobSnippet) {
        UpdateWrapper<SystemJobSnippet> deleteWrapper = new UpdateWrapper<>(systemJobSnippet);
        systemJobSnippetMapper.delete(deleteWrapper);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        systemJobSnippetMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(SystemJobSnippet systemJobSnippet) {
        systemJobSnippetMapper.updateById(systemJobSnippet);
    }

    @Override
    public void updateByIdInBatch(List<SystemJobSnippet> systemJobSnippets) {
        if (CollectionUtils.isEmpty(systemJobSnippets)) {
            return;
        }
        for (SystemJobSnippet systemJobSnippet: systemJobSnippets) {
            this.updateById(systemJobSnippet);
        }
    }

    @Override
    public SystemJobSnippet findById(Integer id) {
        return systemJobSnippetMapper.selectById(id);
    }

    @Override
    public SystemJobSnippet findOne(SystemJobSnippet systemJobSnippet) {
        QueryWrapper<SystemJobSnippet> wrapper = new QueryWrapper<>(systemJobSnippet);
        return systemJobSnippetMapper.selectOne(wrapper);
    }

    @Override
    public List<SystemJobSnippet> findList(SystemJobSnippet systemJobSnippet) {
        QueryWrapper<SystemJobSnippet> wrapper = new QueryWrapper<>(systemJobSnippet);
        return systemJobSnippetMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<SystemJobSnippet> findPage(PageWrap<SystemJobSnippet> pageWrap) {
        IPage<SystemJobSnippet> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<SystemJobSnippet> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.lambda()
                .eq(pageWrap.getModel().getJobId() != null, SystemJobSnippet::getJobId, pageWrap.getModel().getJobId())
                .eq(pageWrap.getModel().getSnippetCode() != null, SystemJobSnippet::getSnippetCode, pageWrap.getModel().getSnippetCode())
                .eq(pageWrap.getModel().getJobDistributeGroup() != null, SystemJobSnippet::getJobDistributeGroup, pageWrap.getModel().getJobDistributeGroup());
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(systemJobSnippetMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(SystemJobSnippet systemJobSnippet) {
        QueryWrapper<SystemJobSnippet> wrapper = new QueryWrapper<>(systemJobSnippet);
        return systemJobSnippetMapper.selectCount(wrapper);
    }
}
